<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/calendar/Save.class.php
 *
 * 保存新增的日历事件
 *
 * PHP Version 5
 *
 * @package:   class.calendar
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

include_once CLASS_PATH.'calendar/CalendarUtil.class.php';
include_once CLASS_PATH.'user/UserUtil.class.php';


class Save extends BaseAction {

   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 保存新增的日历事件
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      //获取参数
      //事件的类型
      $type = $this->getParameterFromPOST('type');

      if ( !$type && 
         $type != 'event' &&
         $type != 'dating' &&
         $type != 'plan' ) {
         $type = 'event';
      }
      
      //创建此事件的日期
      $create_year = $this->getParameterFromPOST('cyear');
      $create_month = $this->getParameterFromPOST('cmonth');
      $create_day = $this->getParameterFromPOST('cday');

      $now = getdate();    //现在的日期

      //检测日期是否合法，如果不合法，则采取现在的日期
      if ( !checkdate($create_month, $create_day, $create_year) ) {
         $create_year = $now['year'];
         $create_month = $now['mon'];
         $cteate_day = $now['mday'];
      }

      $now_time = $now['hours'].':'.$now['minutes'].':'.$now['seconds'];

      $now_create_time = $create_year.'-'.$create_month.'-'.$create_day.' '.$now_time;

      //时间安排的时间和事件结束的时间
      $begin_year = $this->getParameterFromPOST('byear');
      $begin_month = $this->getParameterFromPOST('bmonth');
      $begin_day = $this->getParameterFromPOST('bday');

      if ( !checkdate($begin_month, $begin_day, $begin_year) ) {
         $begin_year = $now['year'];
         $begin_month = $now['mon'];
         $begin_day = $now['mday'];
      }

      $begin_time = $begin_year.'-'.$begin_month.'-'.$begin_day.' 00:00:00';
      $begin_c_time = mktime(0, 0, 0, $begin_month, $begin_day, $begin_year);


      //结束的时间
      $end_year = $this->getParameterFromPOST('eyear');
      $end_month = $this->getParameterFromPOST('emonth');
      $end_day = $this->getParameterFromPOST('eday');

      if ( !checkdate($end_month, $end_day, $end_year) ) {
         $end_year = $now['year'];
         $end_month = $now['mon'];
         $end_day = $now['mday'];
      }

      $end_time = $end_year.'-'.$end_month.'-'.$end_day.' 23:59:59';
      $end_c_time = mktime(23, 59, 59, $end_month, $end_day, $end_year);

      //如果结束时间小于开始时间，则
      //交换一下
      if ( $end_c_time < $begin_c_time ) {
         list($end_time, $begin_time) = each($begin_time, $end_time);
      }

      //看看是否使用邮件提醒和短消息提醒
      $email_note = $this->getParameterFromPOST('email');
      $message_node = $this->getParameterFromPOSt('msg');

      //事件的内容
      $content = $this->getParameterFromPOST('content');

      if ( !$content || strlen($content) <= 0 ) {
         $this->AlertAndBack('内容不能为空。');
         return;
      }

      //事件的标题
      $title = $this->getParameterFromPOST('title');

      if ( !$title ) {
         switch ( $type ) {
         case 'event':
            $title = '未命名事件';
            break;
         case 'dating':
            $title = '未命名约会';
            break;
         case 'plan':
            $title = '未命名计划';
            break;
         }
      }

      $accesstype = $this->getParameterFromPOST('accesstype');

      if ( $accesstype != 'public' && $accesstype != 'private' ) {
         $accesstype = 'public';
      }


      //如果这一切都正常，我们将存储这些数据
      //如果用户已经登录，则取得用户的id
      $user_id = 0;
      if ( $_SESSION['user']['name'] ) {
         $user_id = UserUtil::getUserId($this->db, $_SESSION['user']['name']); 
      } else {
         $user_id = 0;
      }

      $this->db->debug = 1;
      $sql = 'insert into calendar (user_id, type, createtime, fromtime, endtime, '.
         'emailremind, messageremind, content, title, accesstype ) values ( ?, ?, ?, ?, '.
         '?, ?, ?, ?, ?, ?)';
      


      $sth = $this->db->prepare($sql);
      $this->db->Execute($sth, array(
         $user_id,
         $type,
         $now_create_time,
         $begin_time, 
         $end_time,
         $email_note>0 ? 1: 0,
         $message_note>0 ? 1: 0,
         $content,
         $title, 
         $accesstype
      ));

      //看是否存储成功
      if ( $this->db->ErrorNo() ) {
         $this->AlertAndBack($this->db->ErrorMsg() );
         return;
      } else {
         $now_time = mktime(0, 0, 0, $create_month, $create_day, $create_year);
         $this->forward('index.php?module=calendar&action=showday&time='.$now_time);
         return;
      }
   }

}

?>
